package com.trkj.push;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.baidu.android.pushservice.PushMessageReceiver;
import com.trkj.base.Constants;
import com.trkj.base.OnResponseResultListener;
import com.trkj.base.newwork.PostWaitingDataFromRemote;
import com.trkj.base.newwork.WaitingDataFromRemote;
import com.trkj.base.utils.MD5Util;
import com.trkj.base.utils.UserService;

import java.text.MessageFormat;
import java.util.List;

/**
 * Created by LiZanhong on 2015/07/07.
 */
public class NoticePushMessageReceiver extends PushMessageReceiver {
    @Override
    public void onBind(Context context, int i, String s, String s1, String s2, String s3) {
        String responseString = "onBind errorCode=" + i + " appid="
                + s + " userId=" + s1 + " channelId=" + s2
                + " requestId=" + s3;
        Log.d("onBind", responseString);
    }

    @Override
    public void onUnbind(Context context, int i, String s) {

    }

    @Override
    public void onSetTags(Context context, int i, List<String> list, List<String> list1, String s) {
        String responseString = "onSetTags errorCode=" + i + " sucessTags="
                + list + " failTags=" + list + " requestId="
                + s;
        Log.d("onSetTags", responseString);
    }

    @Override
    public void onDelTags(Context context, int i, List<String> list, List<String> list1, String s) {

    }

    @Override
    public void onListTags(Context context, int i, List<String> list, String s) {

    }

    @Override
    public void onMessage(Context context, String s, String s1) {
        //Toast.makeText(context, s, Toast.LENGTH_LONG).show();
    }

    @Override
    public void onNotificationClicked(final Context context, String title, final String content, String s2) {
        WaitingDataFromRemote dataFromRemote =
                new WaitingDataFromRemote(context);
        dataFromRemote.setResponseResultListener(new OnResponseResultListener() {
            @Override
            public void onResponseResult(String result) {
                Log.d("result", result);

                Intent intent = new Intent(NotificationActivity.ACTION);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            }
        });

        String md5 = MD5Util.MD5(title + content);

        Integer uid = UserService.getInstance(context).parseUser().userId;
        if(uid != null){
            String url = MessageFormat.format(Constants.ApiUrl.PUSH_MESSAGE_STATUS_CHANGED, uid, md5);
            dataFromRemote.execute(url);
        }
    }

    @Override
    public void onNotificationArrived(Context context, String title, String content, String s2) {
        Log.d("onNotificationArrived", "executed");
        PostWaitingDataFromRemote dataFromRemote =
                new PostWaitingDataFromRemote(context);
        dataFromRemote.setResponseResultListener(new OnResponseResultListener() {
            @Override
            public void onResponseResult(String result) {
                Log.d("message.md5", result);
            }
        });

        Integer uid = UserService.getInstance(context).parseUser().userId;
        Log.d("uid", String.valueOf(uid));


        if(uid != null) {
            String params = "title=" + title
                    + "&content=" + content
                    + "&uid=" + uid;

            dataFromRemote.execute(Constants.ApiUrl.SAVE_PUSH_MESSAGE_TO_SERVER, params);
        }else{
            Log.d("onNotificationArrived", "没有登陆，无法保存消息。");
        }
    }
}
